package dao.impl;

import dao.BaseDao;
import dao.EmployeeDao;
import entity.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeImpl extends BaseDao implements EmployeeDao {
    @Override
    public List<Employee> findAll() {
        List<Employee> employees = new ArrayList<Employee>();
        Connection conn = getConnection();
        System.out.println("数据库是否连接成功："+(conn != null? "成功":"失败"));
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from employee";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs!=null) {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setName(rs.getString("name"));
                    employee.setPassword(rs.getString("password"));
                    employee.setImage(rs.getString("image"));
                    employee.setBirthday(rs.getString("birthday"));
                    employee.setSalary(rs.getString("salary"));
                    employee.setDepId(rs.getInt("depId"));
                    employee.setCreateTime(rs.getString("createTime"));
                    employee.setUpdateTime(rs.getString("updateTime"));
                    employee.setStart(rs.getInt("start"));
                    employees.add(employee);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return employees;
    }

    @Override
    public List<Employee> findByName(String name) {
        List<Employee> employees = new ArrayList<>();
        Connection conn = getConnection();
        String sql = "select * from employee where name like concat('%',?,'%')";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs!=null) {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setName(rs.getString("name"));
                    employee.setPassword(rs.getString("password"));
                    employee.setImage(rs.getString("image"));
                    employee.setBirthday(rs.getString("birthday"));
                    employee.setSalary(rs.getString("salary"));
                    employee.setDepId(rs.getInt("depId"));
                    employee.setCreateTime(rs.getString("createTime"));
                    employee.setUpdateTime(rs.getString("updateTime"));
                    employee.setStart(rs.getInt("start"));
                    employees.add(employee);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return employees;
    }

    @Override
    public int insert(Employee employee) {
        Connection conn = getConnection();
        String sql = "INSERT INTO employee (\n" +
                "    name, \n" +
                "    password, \n" +
                "    image, \n" +
                "    birthday, \n" +
                "    salary, \n" +
                "    depId, \n" +
                "    createTime, \n" +
                "    updateTime, \n" +
                "    start\n" +
                ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, employee.getName());
            ps.setString(2, employee.getPassword());
            ps.setString(3, employee.getImage());
            ps.setString(4, employee.getBirthday());
            ps.setString(5, employee.getSalary());
            ps.setInt(6, employee.getDepId());
            ps.setString(7, employee.getCreateTime());
            ps.setString(8, employee.getUpdateTime());
            ps.setInt(9, employee.getStart());
            int rows = ps.executeUpdate();
            return rows;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee getById(int empId) {
        Connection conn = getConnection();
        String sql = "select * from employee where id = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, empId);
            rs = ps.executeQuery();

            // 如果查询到结果，将其封装成Employee对象
            if (rs.next()) {
                Employee employee = new Employee();
                employee.setId(rs.getInt("id"));
                employee.setName(rs.getString("name"));
                employee.setPassword(rs.getString("password"));
                employee.setImage(rs.getString("image"));
                employee.setBirthday(rs.getString("birthday"));
                employee.setSalary(rs.getString("salary"));
                employee.setDepId(rs.getInt("depId"));
                employee.setCreateTime(rs.getString("createTime"));
                employee.setUpdateTime(rs.getString("updateTime"));
                employee.setStart(rs.getInt("start"));
                return employee;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("根据ID查询员工信息失败", e);
        } finally {
            close(rs);
        }
        return null;
    }

    @Override
    public int delete(int empId) {
        Connection conn = getConnection();
        String sql = "delete from employee where id = ?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, empId);
            int rows = ps.executeUpdate();
            return rows;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(null);
        }
    }

    @Override
    public int update(Employee employee) {
        Connection conn = getConnection();
        String sql = "UPDATE employee SET " +
                "    name = ?, " +
                "    password = ?, " +
                "    image = ?, " +
                "    birthday = ?, " +
                "    salary = ?, " +
                "    depId = ?, " +
                "    updateTime = ?, " +
                "    start = ? " +
                "WHERE id = ?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, employee.getName());
            ps.setString(2, employee.getPassword());
            ps.setString(3, employee.getImage());
            ps.setString(4, employee.getBirthday());
            ps.setString(5, employee.getSalary());
            ps.setInt(6, employee.getDepId());
            ps.setString(7, employee.getUpdateTime());
            ps.setInt(8, employee.getStart());
            ps.setInt(9, employee.getId());

            int rows = ps.executeUpdate();
            return rows;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("更新员工信息失败", e);
        } finally {
            close(null);
        }
    }
}
